package main

import "fmt"

// https://leetcode.cn/problems/jump-game/?envType=study-plan-v2&envId=top-interview-150l
// 注意点：什么时候判断当前节点无法到达
func main() {
	nums := []int{2, 3, 1, 1, 4}
	nums = []int{3, 2, 1, 0, 4}
	nums = []int{0}
	res := canJump(nums)
	fmt.Println(res)
}

func canJump(nums []int) bool {
	farest := make([]int, len(nums))
	for i := range nums {
		if i == 0 {
			farest[i] = nums[i]
			continue
		}
		if farest[i-1] < i {
			return false
		}
		farest[i] = max(nums[i]+i, farest[i-1])
	}
	l := len(nums)
	return farest[l-1] >= (l - 1)
}

func max(v1, v2 int) int {
	if v1 > v2 {
		return v1
	}
	return v2
}
